package com.wj.service.impl;

import com.wj.domain.dto.UserDTO;
import com.wj.domain.entity.UserEntity;
import com.wj.domain.vo.UserVO;
import com.wj.infrastructure.repository.UserRepository;
import com.wj.service.UserService;
import com.wj.tool.common.utils.UuidUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Override
    public boolean verificationUser(String username, String password) {
        UserEntity userEntity = userRepository.findByUsernameAndPassword(username, password);
        if (userEntity != null){
            return true;
        }
        return false;
    }

    @Override
    public boolean verificationUserName(String username) {
        UserEntity userEntity = userRepository.findByUsername(username);
        if (userEntity != null){
            return true;
        }
        return false;
    }

    @Override
    public UserVO getUserByName(String username) {
        UserVO userVO = new UserVO();
        UserEntity userEntity = userRepository.findByUsername(username);
        if (userEntity != null){
            BeanUtils.copyProperties(userEntity, userVO);
        }
        return userVO;
    }

    @Override
    public void save(UserDTO userDTO) {
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userDTO, userEntity);
        userEntity.setId(UuidUtils.createId());
        userEntity.setStatus(1);
        userRepository.save(userEntity);
    }
}